#include "errors.h"
#include <errno.h>
#include <string>

std::string errormsg = "OK";

void set_error(const char * _errmsg) {
	errormsg = std::string(_errmsg);
}

void auto_set_error(const char * _place) {
	errormsg = std::string(_place);
	errormsg.append(": ");
	if(errno == EACCES) {
		errormsg.append("Write permission denied.");
	}
	else if(errno == EAGAIN) {
		errormsg.append("Temporarily unavailable.");
	}
	else if(errno == EBADF) {
		errormsg.append("Bad file descriptor.");
	}
	else if(errno == ECONNRESET) {
		errormsg.append("Connection reset.");
	}
	else if(errno == EDESTADDRREQ) {
		errormsg.append("Not in connection mode.");
	}
	else if(errno == EFAULT) {
		errormsg.append("Invalid user space address.");
	}
	else if(errno == EINTR) {
		errormsg.append("Connection interrupted by signal.");
	}
	else if(errno == EINVAL) {
		errormsg.append("Invalid argument.");
	}
	else if(errno == EISCONN) {
		errormsg.append("Already connected.");
	}
	else if(errno == ENOBUFS) {
		errormsg.append("Network output queue is full.");
	}
	else if(errno == ENOMEM) {
		errormsg.append("No memory available.");
	}
	else if(errno == ENOTCONN) {
		errormsg.append("Socket is not connected.");
	}
	else if(errno == ENOTSOCK) {
		errormsg.append("Not a socket.");
	}
	else if(errno == EADDRINUSE) {
		errormsg.append("Address is already in use.");
	}
	else if(errno == EADDRNOTAVAIL) {
		errormsg.append("Address/interface not available.");
	}
	else if(errno == ELOOP) {
		errormsg.append("Too many symbolic links when resolving address.");
	}
	else if(errno == ENAMETOOLONG) {
		errormsg.append("Address is too long.");
	}
	else if(errno == ENOENT) {
		errormsg.append("File does not exist.");
	}
	else {
		errormsg.append("Unknown error.");
	}
}

const char * get_error() {
	return errormsg.c_str();
}
